<div id="formDiv">

</div>
<script type="text/html" id="formContent">
    <form lay-filter="exchangeRecordsForm" class="layui-form model-form systemAppForm" id="exchangeRecordsForm">
        <input name="id" type="hidden"/>
        <div class="layui-col-md12">
            <label class="layui-form-label">APK下载地址</label>
            <div class="layui-input-inline">
                <div id="productPicBtn1" class="layui-btn" style="float: left">
                    <i class="layui-icon">&#xe681;</i>上传app
                </div>
                <input name="downFileUrl" type="hidden" class="layui-input" lay-verify="" id="downFileUrl"/>
                <input name="fileName" type="hidden" class="layui-input" lay-verify="" id="fileName"/>
                <!--                    <div id="fileNameDiv" style="cursor: default"></div>-->
            </div>
        </div>
        <div class="layui-col-md12">
            <label class="layui-form-label">版本名称</label>
            <div class="layui-input-block">
                <input class="layui-input" name="versionName" lay-filter="type" lay-verify="required"/>
            </div>
        </div>

        <div class="layui-col-md12">
            <label class="layui-form-label">版本号</label>
            <div class="layui-input-block">
                <input class="layui-input" name="version" lay-filter="type" lay-verify="required"/>
            </div>
        </div>

        <div class="layui-col-md12">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <select id="type" name="type" lay-filter="type" lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-col-md12">
            <label class="layui-form-label">版本更新内容</label>
            <div class="layui-input-block">
                <textarea class="layui-input" name="remark" lay-filter="type" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <div class="right-btn">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="productDetailSubmit" id="submitBtn" lay-submit>提交</button>
            </div>
        </div>
    </form>
</script>

<script>
    layui.use(['layer', 'form', 'table', 'admin', 'upload', 'tree', 'laytpl'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laytpl = layui.laytpl;
        laytpl(formContent.innerHTML).render({}, function (html) {
            $('#formDiv').html(html);
        })
        $.each($('#exchangeRecordsForm').find('input, select, #submitBtn, #productPicBtn1'), function () {
            $(this).attr('disabled', parentData.eventType == 'detail');
        })
        data = parentData.data ? JSON.parse(JSON.stringify(parentData.data)) : ''
        if (data) {
            admin.getDictList('type', 'app_use_type', data.type, '类型')
            form.val('exchangeRecordsForm', data)
        } else {
            admin.getDictList('type', 'app_use_type', null, '类型')
        }

        // 表单提交事件
        form.on('submit(productDetailSubmit)', function (d) {
            let formData = {...d.field}
            if ($('#downFileUrl').val()) {
                admin.req(formData.id ? 'enterprise/system/admin/apkVersionManager/update' : 'enterprise/system/admin/apkVersionManager/add',
                    JSON.stringify(formData)
                    , function (res) {
                        if (res.code == 200) {
                            layer.msg('添加成功');
                            layer.closeAll();
                            table.reload('tableDiv')
                        } else {
                            layer.msg(res.msg);
                        }
                    }, 'POST', true, 'application/json; charset=utf-8')
            } else {
                layer.msg('请上传apk');
            }
            console.log('data', formData)

            return false;
        });


        imgUpload()

        function imgUpload() {
            upload.render({
                elem: parentData.eventType == 'detail' ? '' : '#productPicBtn1',
                url: config.upload_url + 'uploadFile',
                accept: 'file',
                headers: {Authorization: 'Bearer ' + config.getToken()},
                // acceptMime: 'image/*',
                exts: 'apk|ap',
                before: function () {
                    // 显示加载动画
                    loadingIndex = layer.load(2, {shade: [0.3, '#fff']}); // 0.3 表示遮罩透明度
                },
                done: function (res) {
                    // On successful upload
                    if (res.code === 200) {
                        layer.close(loadingIndex);
                        $('#downFileUrl').val(res.data.url); // Store the image URL in hidden input
                        $('#fileName').val(res.data.fileName); // Store the image URL in hidden input
                        // $('#fileNameDiv').text(res.data.fileName);
                        layer.msg('apk上传成功');
                    } else {
                        layer.close(loadingIndex);
                        layer.msg('apk上传失败: ' + res.msg);
                    }
                },
                error: function () {
                    // On upload failure
                    layer.msg('上传失败，请重试');
                }
            });
        }
    })


</script>

<style scoped>
    .systemAppForm .layui-form-label {
        width: 100px;
    }
    .systemAppForm .layui-input-block{
        margin-left: 130px;
    }

    .layui-col-md12 {
        margin-bottom: 20px;
    }
</style>